package com.xiaoshi.translator;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.xiaoshi.utils.Base64Utils;
import com.xiaoshi.utils.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author sen
 * @description  base解密     @JsonDeserialize(using= DecryptDeserializer.class)
 * @date 2023/4/26 11:06
 */

public class DecryptDeserializer extends JsonDeserializer<String> {
    private static final Logger log = LoggerFactory.getLogger(DecryptDeserializer.class);

    public DecryptDeserializer() {
    }

    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        return this.replaceStr(jsonParser.getText());
    }

    private String replaceStr(String str) {
        log.debug("执行反序列化");
        return ObjectUtils.isEmpty(str) ? str : Base64Utils.decode(str);
    }
}
